package org.thulearn.test;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class Login {
	public static String STR="hello world";
	
	public static String login(String[] args) {
		HttpClient client = new DefaultHttpClient();
//		HttpClient client1 = new DefaultHttpClient();
		HttpPost post = new HttpPost("https://learn.tsinghua.edu.cn/MultiLanguage/lesson/teacher/loginteacher.jsp");
		//HttpGet get = new HttpGet("http://learn.tsinghua.edu.cn/MultiLanguage/lesson/student/MyCourse.jsp?language=cn");
		//HttpGet get = new HttpGet("http://learn.tsinghua.edu.cn/MultiLanguage/lesson/student/hom_wk_brw.jsp?course_id=84898");
		HttpGet get = new HttpGet("http://learn.tsinghua.edu.cn/MultiLanguage/vspace/vspace_userinfo1.jsp");
		List<NameValuePair> form = new ArrayList<NameValuePair>();
//		form.add(new BasicNameValuePair("userid","ghc11"));
//		form.add(new BasicNameValuePair("userpass","ghcnn10120905"));
		form.add(new BasicNameValuePair("userid","flg11"));
		form.add(new BasicNameValuePair("userpass","fan139sha0311"));
		try {
			UrlEncodedFormEntity uef = new UrlEncodedFormEntity(form,"UTF-8");
			post.setEntity(uef);
			System.out.println(post.getURI());
			
			client.execute(post);
			post.abort();
			
//			ResponseHandler<String> res = new BasicResponseHandler();
//			String str = client.execute(get, res);
//			System.out.println(str);
			HttpResponse res = client.execute(get);
			HttpEntity ent = res.getEntity();
			if(ent!=null){
				STR=EntityUtils.toString(ent,"utf-8");
				//System.out.println(STR);
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally{
			client.getConnectionManager().shutdown();
		}
		return STR;
	}
}
